package com.travelsky.app.util.file;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.travelsky.app.util.validate.ValidateUtil;

public class FileUtil {
    //static Log log = LogFactory.getLog(FileUtil.class);
    private static Logger log = Logger.getLogger(FileUtil.class.getName());
	
	public static String getRootPath(){
		String rootPath = "";
		try {
			URL base = FileUtil.class.getResource("");
			rootPath = new File(base.getFile(), "../../../../../../../../").getCanonicalPath();				
 			rootPath=java.net.URLDecoder.decode(rootPath,"UTF-8");
		} catch (IOException e) { 
			e.printStackTrace();
		}			
		return rootPath;
	}
	
	public static List getAllFilesByDir(String filePath){
		List fileNameList = new ArrayList();
		File path = new File(filePath);

		if (!path.exists()) {
			log.info("文件名称不存在!");
			return fileNameList;
		}

		if (path.isFile()) {
			fileNameList.add(path.getName());
			log.info(path);
			log.info(path.getName());
			return fileNameList;
		}

		File[] files = path.listFiles();
		for (int i = 0; i < files.length; i++) {
			if(files[i].isFile()){
				fileNameList.add(files[i].getName());
				log.info(files[i].getName());
			}
		}
		return fileNameList;
	} 
//	//add by xuxu 0414
//	public static boolean createTxtFile(String filePath,List list,Date date) throws Exception{
//		BufferedWriter writer = null;
//		try{
//			 writer = new  BufferedWriter( new FileWriter(filePath));
//			 int num = ValidateUtil.isNullOrEmpty(list)?0:list.size();
//			 writer.write("Date#"+DateUtil.toString(date,DateUtil.yyyy_MM_dd)+"#Count#"+num);
//			 writer.newLine();
//			 writer.newLine();
//			 Iterator it = (Iterator) list.iterator();
//			 while(it.hasNext()){
//				 PaxSegVO vo = (PaxSegVO) it.next();
//				 Long point  = vo.getPointGift()==null?0L:vo.getPointGift();
//				 String userName = ValidateUtil.isNullOrEmpty(vo.getUserName())?"":vo.getUserName();
//				 String ffpNo = ValidateUtil.isNullOrEmpty(vo.getFfpNo())?"":vo.getFfpNo();
//				 writer.write(vo.getOrdNo() + "#" + vo.getPnrNo() + "#"
//						+ DateUtil.toString(vo.getOptDt(), DateUtil.yyyy_MM_dd_HH_mm_ss) + "#"
//						+ vo.getPaxName() + "#" + vo.getFlightNo() + "#"
//						+ DateUtil.toString(vo.getDeptDate(), DateUtil.yyyy_MM_dd_HH_mm) + "#"
//						+ vo.getOrgApcd() + "#" + vo.getDstApcd() + "#" + vo.getCabinCode() + "#"
//						+ point + "#" + vo.getTktNo() + "#" + userName + "#"
//						+ ffpNo);
//				 writer.newLine();
//			 }
//			 log.info("创建文件成功："+filePath);
//		}catch(IOException e){
//			writer.flush();
//			writer.close();
//			log.error("创建文件:"+e);
//			return false;
//		}finally{
//			writer.flush();
//			writer.close();
//		}
//		return true;
//	}
	
	public static void dealTab(String str){
		if(ValidateUtil.isNullOrEmpty(str))str="";
	}
}
